﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Xceed.Silverlight.DataGrid
{
  #region metadata

  [TemplateVisualState( GroupName = "CommitStates", Name = "NoPendingCommit" )]
  [TemplateVisualState( GroupName = "CommitStates", Name = "CommitPending" )]

  #endregion
  public sealed class CommittingIndicator : IndicatorBase
  {
    public CommittingIndicator()
      : base()
    {
      this.DefaultStyleKey = typeof( CommittingIndicator );

      this.UpdateCommitStates( false );
    }

    #region CommitPending Property

    public static readonly DependencyProperty CommitPendingProperty = DependencyProperty.Register(
      "CommitPending",
      typeof( bool ),
      typeof( CommittingIndicator ),
      new PropertyMetadata( false,
        new PropertyChangedCallback( CommittingIndicator.OnCommitPendingChanged ) ) );

    public bool CommitPending
    {
      get
      {
        return ( bool )this.GetValue( CommittingIndicator.CommitPendingProperty );
      }
      set
      {
        this.SetValue( CommittingIndicator.CommitPendingProperty, value );
      }
    }

    private static void OnCommitPendingChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
      CommittingIndicator indicator = sender as CommittingIndicator;

      if( indicator == null )
        return;

      indicator.UpdateCommitStates( true );
    }

    #endregion

    internal override void OnKillTimerTick( object sender, EventArgs e )
    {
      Debug.Assert( !this.CommitPending, "Killing CommitIndicator while CommitPending is true" );

      base.OnKillTimerTick( sender, e );
    }

    private void UpdateCommitStates( bool useTransitions )
    {
      if( this.CommitPending )
      {
        VisualStateManager.GoToState( this, "CommitPending", useTransitions );

        this.StartTimer();
        this.StopKillTimer();
      }
      else
      {
        VisualStateManager.GoToState( this, "NoPendingCommit", useTransitions );

        this.StartKillTimer();
      }
    }
  }
}
